package util;


import java.sql.*;
import java.lang.reflect.*;
import java.io.*;
import xpubbean.Db.*;


public class TableUtil {
    public static void main(String args[]) throws Exception {
        Connection conn = new Conn().getConnection();

        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select t1.table_name, t3.column_name from user_tables t1, user_constraints t2, user_cons_columns t3 " +
            "where t1.table_name = t2.table_name and t2.table_name = t3.table_name and " +
            "      t2.constraint_name = t3.constraint_name and t2.constraint_type = 'P' ");

        while (rs.next()) {
            String tableName = rs.getString("table_name");
            String keyName = rs.getString("column_name");

            String path = "e:\\entity\\";

            File file = new File(path + tableName + ".java");
            java.io.RandomAccessFile raf = new RandomAccessFile(file, "rw");
            raf.writeBytes("package com.sino.tb.entity;\n");
            raf.writeBytes("\n");
            raf.writeBytes("\n");

            raf.writeBytes("public class " + tableName + " { \n");

            raf.writeBytes("  public final String mainKey = \"" + keyName +
                "\"; \n");

            String sql = "select * from " + tableName + " where 1=2";
            Statement stmt1 = conn.createStatement();
            ResultSet rs1 = stmt1.executeQuery(sql);
            ResultSetMetaData rmd = rs1.getMetaData();

            for (int i = 1; i <= rmd.getColumnCount(); i++) {
                String columnName = rmd.getColumnName(i);
                raf.writeBytes("  private String " + columnName.toLowerCase() +
                    "; \n");
            }
            raf.writeBytes("\n");
            raf.writeBytes("\n");

            for (int i = 1; i <= rmd.getColumnCount(); i++) {
                String columnName = rmd.getColumnName(i);

                raf.writeBytes("  public void set" + columnName.toLowerCase() +
                    "(String " +
                    columnName.toLowerCase() + ") { \n");
                raf.writeBytes("    this." + columnName.toLowerCase() + " = " +
                    columnName.toLowerCase() + ";\n");
                raf.writeBytes("  } \n\n");

            }

            raf.writeBytes("\n");
            raf.writeBytes("\n");

            for (int i = 1; i <= rmd.getColumnCount(); i++) {
                String columnName = rmd.getColumnName(i);
                raf.writeBytes("  public String get" + columnName.toLowerCase() +
                    "() { \n");
                raf.writeBytes("    return " + columnName.toLowerCase() +
                    "; \n");
                raf.writeBytes("  } \n\n");
            }

            raf.writeBytes("\n");
            raf.writeBytes("\n");

            raf.writeBytes("} \n");
            raf.close();
        }
        rs.close();
        stmt.close();
        conn.close();
//
//    T_SEC_ORG user = (T_SEC_ORG) load("10180", "T_SEC_ORG");
//    System.out.println(user.getc_org_name());
//    conn.close();
    }

}
